"""
Micropython (Raspberry Pi Pico)
2022/1/12     DENGFEI
lcd.Display() Only 94 limited characters in fonts can be displayed
品牌：keyes2
"""
'''
from lcd128_32 import lcd128_32

#i2c配置
clock_pin = 22
data_pin = 21
bus = 0
i2c_addr = 0x3f # 自己指定地址


lcd = lcd128_32(data_pin, clock_pin, bus, i2c_addr)
lcd.Clear()

lcd.Cursor(0, 4) 
lcd.Display("KEYES2")
lcd.Cursor(1, 0)
lcd.Display("ABCDEFGHIJKLMNOPQR") # 一行18个
lcd.Cursor(2, 0)
lcd.Display("123456789+-*/<>=$@")
lcd.Cursor(3, 0)
lcd.Display("%^&(){}:;'|?,.~\\[]")
'''
import machine
import time

class Lcd128_32:
    
    def __init__(self,dt,clk,bus=0,addr=0x3f):
        self.addr = addr
        self.i2c = machine.I2C(bus,sda=machine.Pin(dt),scl=machine.Pin(clk))
        self.cur = [0, 0]
        lcd128_32_fonts = {
            0:[0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E, 0x00],
            1:[0x00, 0x00, 0x42, 0x7F, 0x40, 0x00, 0x00],
            2:[0x00, 0x62, 0x51, 0x49, 0x49, 0x46, 0x00],
            3:[0x00, 0x21, 0x41, 0x49, 0x4D, 0x33, 0x00],
            4:[0x00, 0x18, 0x14, 0x12, 0x7F, 0x10, 0x00],
            5:[0x00, 0x27, 0x45, 0x45, 0x45, 0x39, 0x00],
            6:[0x00, 0x3C, 0x4A, 0x49, 0x49, 0x31, 0x00],
            7:[0x00, 0x01, 0x71, 0x09, 0x05, 0x03, 0x00],
            8:[0x00, 0x36, 0x49, 0x49, 0x49, 0x36, 0x00],
            9:[0x00, 0x46, 0x49, 0x49, 0x29, 0x1E, 0x00],
            10:[0x00, 0x24, 0x54, 0x54, 0x38, 0x40, 0x00],
            11:[0x00, 0x7F, 0x28, 0x44, 0x44, 0x38, 0x00],
            12:[0x00, 0x38, 0x44, 0x44, 0x44, 0x08, 0x00],
            13:[0x00, 0x38, 0x44, 0x44, 0x28, 0x7F, 0x00],
            14:[0x00, 0x38, 0x54, 0x54, 0x54, 0x08, 0x00],
            15:[0x00, 0x08, 0x7E, 0x09, 0x09, 0x02, 0x00],
            16:[0x00, 0x98, 0xA4, 0xA4, 0xA4, 0x78, 0x00],
            17:[0x00, 0x7F, 0x08, 0x04, 0x04, 0x78, 0x00],
            18:[0x00, 0x00, 0x00, 0x79, 0x00, 0x00, 0x00],
            19:[0x00, 0x00, 0x80, 0x88, 0x79, 0x00, 0x00],
            20:[0x00, 0x7F, 0x10, 0x28, 0x44, 0x40, 0x00],
            21:[0x00, 0x00, 0x41, 0x7F, 0x40, 0x00, 0x00],
            22:[0x00, 0x78, 0x04, 0x78, 0x04, 0x78, 0x00],
            23:[0x00, 0x04, 0x78, 0x04, 0x04, 0x78, 0x00],
            24:[0x00, 0x38, 0x44, 0x44, 0x44, 0x38, 0x00],
            25:[0x00, 0xFC, 0x24, 0x24, 0x24, 0x18, 0x00],
            26:[0x00, 0x18, 0x24, 0x24, 0x24, 0xFC, 0x00],
            27:[0x00, 0x04, 0x78, 0x04, 0x04, 0x08, 0x00],
            28:[0x00, 0x48, 0x54, 0x54, 0x54, 0x24, 0x00],
            29:[0x00, 0x04, 0x3F, 0x44, 0x44, 0x24, 0x00],
            30:[0x00, 0x3C, 0x40, 0x40, 0x3C, 0x40, 0x00],
            31:[0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C, 0x00],
            32:[0x00, 0x3C, 0x40, 0x3C, 0x40, 0x3C, 0x00],
            33:[0x00, 0x44, 0x28, 0x10, 0x28, 0x44, 0x00],
            34:[0x00, 0x9C, 0xA0, 0xA0, 0x90, 0x7C, 0x00],
            35:[0x00, 0x44, 0x64, 0x54, 0x4C, 0x44, 0x00],
            36:[0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C, 0x00],
            37:[0x00, 0x7F, 0x49, 0x49, 0x49, 0x36, 0x00],
            38:[0x00, 0x3E, 0x41, 0x41, 0x41, 0x22, 0x00],
            39:[0x00, 0x7F, 0x41, 0x41, 0x41, 0x3E, 0x00],
            40:[0x00, 0x7F, 0x49, 0x49, 0x49, 0x41, 0x00],
            41:[0x00, 0x7F, 0x09, 0x09, 0x09, 0x01, 0x00],
            42:[0x00, 0x3E, 0x41, 0x51, 0x51, 0x72, 0x00],
            43:[0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F, 0x00],
            44:[0x00, 0x00, 0x41, 0x7F, 0x41, 0x00, 0x00],
            45:[0x00, 0x20, 0x40, 0x41, 0x3F, 0x01, 0x00],
            46:[0x00, 0x7F, 0x08, 0x14, 0x22, 0x41, 0x00],
            47:[0x00, 0x7F, 0x40, 0x40, 0x40, 0x40, 0x00],
            48:[0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F, 0x00],
            49:[0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F, 0x00],
            50:[0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E, 0x00],
            51:[0x00, 0x7F, 0x09, 0x09, 0x09, 0x06, 0x00],
            52:[0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E, 0x00],
            53:[0x00, 0x7F, 0x09, 0x19, 0x29, 0x46, 0x00],
            54:[0x00, 0x26, 0x49, 0x49, 0x49, 0x32, 0x00],
            55:[0x00, 0x01, 0x01, 0x7F, 0x01, 0x01, 0x00],
            56:[0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F, 0x00],
            57:[0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F, 0x00],
            58:[0x00, 0x7F, 0x20, 0x18, 0x20, 0x7F, 0x00],
            59:[0x00, 0x63, 0x14, 0x08, 0x14, 0x63, 0x00],
            60:[0x00, 0x03, 0x04, 0x78, 0x04, 0x03, 0x00],
            61:[0x00, 0x61, 0x51, 0x49, 0x45, 0x43, 0x00],
            62:[0x00, 0x00, 0x00, 0x5F, 0x00, 0x00, 0x00],
            63:[0x00, 0x00, 0x07, 0x00, 0x07, 0x00, 0x00],
            64:[0x00, 0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00],
            65:[0x00, 0x24, 0x2E, 0x7B, 0x2A, 0x12, 0x00],
            66:[0x00, 0x23, 0x13, 0x08, 0x64, 0x62, 0x00],
            67:[0x00, 0x36, 0x49, 0x56, 0x20, 0x50, 0x00],
            68:[0x00, 0x00, 0x04, 0x03, 0x01, 0x00, 0x00],
            69:[0x00, 0x00, 0x1C, 0x22, 0x41, 0x00, 0x00],
            70:[0x00, 0x00, 0x41, 0x22, 0x1C, 0x00, 0x00],
            71:[0x00, 0x22, 0x14, 0x7F, 0x14, 0x22, 0x00],
            72:[0x00, 0x08, 0x08, 0x7F, 0x08, 0x08, 0x00],
            73:[0x00, 0x40, 0x30, 0x10, 0x00, 0x00, 0x00],
            74:[0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00],
            75:[0x00, 0x20, 0x10, 0x08, 0x04, 0x02, 0x00],
            76:[0x00, 0x00, 0x36, 0x36, 0x00, 0x00, 0x00],
            77:[0x00, 0x40, 0x36, 0x36, 0x00, 0x00, 0x00],
            78:[0x00, 0x08, 0x14, 0x22, 0x41, 0x00, 0x00],
            79:[0x00, 0x14, 0x14, 0x14, 0x14, 0x14, 0x00],
            80:[0x00, 0x00, 0x41, 0x22, 0x14, 0x08, 0x00],
            81:[0x00, 0x02, 0x01, 0x59, 0x05, 0x02, 0x00],
            82:[0x00, 0x3E, 0x41, 0x5D, 0x55, 0x5E, 0x00],
            83:[0x00, 0x08, 0x36, 0x41, 0x00, 0x00, 0x00],
            84:[0x00, 0x00, 0x00, 0x77, 0x00, 0x00, 0x00],
            85:[0x00, 0x00, 0x00, 0x41, 0x36, 0x08, 0x00],
            86:[0x00, 0x08, 0x04, 0x08, 0x10, 0x08, 0x00],
            87:[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
            88:[0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00],
            89:[0x00, 0x04, 0x02, 0x7F, 0x02, 0x04, 0x00],
            90:[0x00, 0x08, 0x1C, 0x2A, 0x08, 0x08, 0x00],
            91:[0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x00],
            92:[0x00, 0x7F, 0x7F, 0x41, 0x41, 0x00, 0x00],
            93:[0x00, 0x02, 0x04, 0x08, 0x10, 0x20, 0x00],
            94:[0x00, 0x00, 0x41, 0x41, 0x7F, 0x7F, 0x00],
        }
        
        self.Init()
        
    def WriteByte_command(self, cmd):
        self.reg_write(0x00, cmd)
    
    def WriteByte_dat(self, dat):
        self.reg_write(0x40, dat)
    
    def reg_write(self, reg, data):
        msg = bytearray()
        msg.append(data)
        self.i2c.writeto_mem(self.addr, reg, msg)
    
    def Init(self):
        #self.i2c.start()
        time.sleep(0.01)
        self.WriteByte_command(0xe2)
        time.sleep(0.01)
        self.WriteByte_command(0xa3)
        self.WriteByte_command(0xa0)
        self.WriteByte_command(0xc8)
        self.WriteByte_command(0x22)
        self.WriteByte_command(0x81)
        self.WriteByte_command(0x30)
        self.WriteByte_command(0x2c)
        self.WriteByte_command(0x2e)
        self.WriteByte_command(0x2f)
        self.Clear()
        self.WriteByte_command(0xff)
        self.WriteByte_command(0x72)
        self.WriteByte_command(0xfe)
        self.WriteByte_command(0xd6)
        self.WriteByte_command(0x90)
        self.WriteByte_command(0x9d)
        self.WriteByte_command(0xaf)
        self.WriteByte_command(0x40)
    
    def Clear(self):
        for i in range(4):
            self.WriteByte_command(0xb0 + i)
            self.WriteByte_command(0x10)
            self.WriteByte_command(0x00)
            for j in range(128):
                self.WriteByte_dat(0x00)
    
    def Cursor(self, y, x):
        if x > 17:
            x = 17
        if y > 3:
            x = 3
        self.cursor[0] = y
        self.cursor[1] = x
        
    def WriteFont(self, num):
        lcd128_32_fonts = self.lcd128_32_fonts
        for item in lcd128_32_fonts.textFont[num]:
            self.WriteByte_dat(item)
    
    def Display(self, str):
        cursor = self.cursor
        self.WriteByte_command(0xb0 + cursor[0])
        self.WriteByte_command(0x10 + cursor[1] * 7 // 16)
        self.WriteByte_command(0x00 + cursor[1] * 7 % 16)
        for num in range(len(str)):
            if str[num] == '0':
                self.WriteFont(0)
            elif str[num] == '1':
                self.WriteFont(1)
            elif str[num] == '2':
                self.WriteFont(2)
            elif str[num] == '3':
                self.WriteFont(3)
            elif str[num] == '4':
                self.WriteFont(4)
            elif str[num] == '5':
                self.WriteFont(5)
            elif str[num] == '6':
                self.WriteFont(6)
            elif str[num] == '7':
                self.WriteFont(7)
            elif str[num] == '8':
                self.WriteFont(8)
            elif str[num] == '9':
                self.WriteFont(9)
            elif str[num] == 'a':
                self.WriteFont(10)
            elif str[num] == 'b':
                self.WriteFont(11)
            elif str[num] == 'c':
                self.WriteFont(12)
            elif str[num] == 'd':
                self.WriteFont(13)
            elif str[num] == 'e':
                self.WriteFont(14)
            elif str[num] == 'f':
                self.WriteFont(15)
            elif str[num] == 'g':
                self.WriteFont(16)
            elif str[num] == 'h':
                self.WriteFont(17)
            elif str[num] == 'i':
                self.WriteFont(18)
            elif str[num] == 'j':
                self.WriteFont(19)
            elif str[num] == 'k':
                self.WriteFont(20)
            elif str[num] == 'l':
                self.WriteFont(21)
            elif str[num] == 'm':
                self.WriteFont(22)
            elif str[num] == 'n':
                self.WriteFont(23)
            elif str[num] == 'o':
                self.WriteFont(24)
            elif str[num] == 'p':
                self.WriteFont(25)
            elif str[num] == 'q':
                self.WriteFont(26)
            elif str[num] == 'r':
                self.WriteFont(27)
            elif str[num] == 's':
                self.WriteFont(28)
            elif str[num] == 't':
                self.WriteFont(29)
            elif str[num] == 'u':
                self.WriteFont(30)
            elif str[num] == 'v':
                self.WriteFont(31)
            elif str[num] == 'w':
                self.WriteFont(32)
            elif str[num] == 'x':
                self.WriteFont(33)
            elif str[num] == 'y':
                self.WriteFont(34)
            elif str[num] == 'z':
                self.WriteFont(35)
            elif str[num] == 'A':
                self.WriteFont(36)
            elif str[num] == 'B':
                self.WriteFont(37)
            elif str[num] == 'C':
                self.WriteFont(38)
            elif str[num] == 'D':
                self.WriteFont(39)
            elif str[num] == 'E':
                self.WriteFont(40)
            elif str[num] == 'F':
                self.WriteFont(41)
            elif str[num] == 'G':
                self.WriteFont(42)
            elif str[num] == 'H':
                self.WriteFont(43)
            elif str[num] == 'I':
                self.WriteFont(44)
            elif str[num] == 'J':
                self.WriteFont(45)
            elif str[num] == 'K':
                self.WriteFont(46)
            elif str[num] == 'L':
                self.WriteFont(47)
            elif str[num] == 'M':
                self.WriteFont(48)
            elif str[num] == 'N':
                self.WriteFont(49)
            elif str[num] == 'O':
                self.WriteFont(50)
            elif str[num] == 'P':
                self.WriteFont(51)
            elif str[num] == 'Q':
                self.WriteFont(52)
            elif str[num] == 'R':
                self.WriteFont(53)
            elif str[num] == 'S':
                self.WriteFont(54)
            elif str[num] == 'T':
                self.WriteFont(55)
            elif str[num] == 'U':
                self.WriteFont(56)
            elif str[num] == 'V':
                self.WriteFont(57)
            elif str[num] == 'W':
                self.WriteFont(58)
            elif str[num] == 'X':
                self.WriteFont(59)
            elif str[num] == 'Y':
                self.WriteFont(60)
            elif str[num] == 'Z':
                self.WriteFont(61)
            elif str[num] == '!':
                self.WriteFont(62)
            elif str[num] == '"':
                self.WriteFont(63)
            elif str[num] == '#':
                self.WriteFont(64)
            elif str[num] == '$':
                self.WriteFont(65)
            elif str[num] == '%':
                self.WriteFont(66)
            elif str[num] == '&':
                self.WriteFont(67)
            elif str[num] == '\'':
                self.WriteFont(68)
            elif str[num] == '(':
                self.WriteFont(69)
            elif str[num] == ')':
                self.WriteFont(70)
            elif str[num] == '*':
                self.WriteFont(71)
            elif str[num] == '+':
                self.WriteFont(72)
            elif str[num] == ',':
                self.WriteFont(73)
            elif str[num] == '-':
                self.WriteFont(74)
            elif str[num] == '/':
                self.WriteFont(75)
            elif str[num] == ':':
                self.WriteFont(76)
            elif str[num] == ';':
                self.WriteFont(77)
            elif str[num] == '<':
                self.WriteFont(78)
            elif str[num] == '=':
                self.WriteFont(79)
            elif str[num] == '>':
                self.WriteFont(80)
            elif str[num] == '?':
                self.WriteFont(81)
            elif str[num] == '@':
                self.WriteFont(82)
            elif str[num] == '{':
                self.WriteFont(83)
            elif str[num] == '|':
                self.WriteFont(84)
            elif str[num] == '}':
                self.WriteFont(85)
            elif str[num] == '~':
                self.WriteFont(86)
            elif str[num] == ' ':
                self.WriteFont(87)
            elif str[num] == '.':
                self.WriteFont(88)
            elif str[num] == '^':
                self.WriteFont(89)
            elif str[num] == '_':
                self.WriteFont(90)
            elif str[num] == '`':
                self.WriteFont(91)
            elif str[num] == '[':
                self.WriteFont(92)
            elif str[num] == '\\':
                self.WriteFont(93)
            elif str[num] == ']':
                self.WriteFont(94)

